package br.com.android.productivecenter;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import br.com.android.R;
import br.com.android.modelo.Calendario;
import br.com.android.modelo.Documento;
import br.com.android.persistencia.manipularDados;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.app.AlertDialog;
import android.widget.*;
import android.view.*;
import android.app.*;


public class NovoCompromissoActivity extends Activity{
	
	public manipularDados persistencia;
	public String userName;
	
	private int mYear;
    private int mMonth;
    private int mDay;
    private int mHour;
    private int mMinute;
    private TextView mDateDisplay;
    private TextView mTimeDisplay;
    
    static final int DATE_DIALOG_ID = 0;
    static final int TIME_DIALOG_ID = 1;
    
	public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	        if(getIntent().getStringExtra("userName") != null)
	        	userName = getIntent().getStringExtra("userName");
	        
	        persistencia =   getIntent().getExtras().getParcelable("nelson");
	        
	        setContentView(R.layout.novo_comp);        
	        CarregarTelaCompromisso();
	}
	
	private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }
        };
        
       private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    mHour = hourOfDay;
                    mMinute = minute;
                    updateDisplay2();
                }
        };
	
	 private void updateDisplay() {
	        mDateDisplay.setText(
	            new StringBuilder()
	                    // Month is 0 based so add 1
	                    .append(mDay).append("/")
	                    .append(mMonth +1).append("/")
	                    .append(mYear).append(" "));
	    }
	 
	 private void updateDisplay2() {
	        mTimeDisplay.setText(
	            new StringBuilder()
	                    // Month is 0 based so add 1
	                    .append(mHour).append(":")
	                    .append(mMinute).append(" "));
	    }
	 
	 public void CarregarTelaCompromisso(){
		 setContentView(R.layout.novo_comp);
		 
		 final Calendar c = Calendar.getInstance();
	     mYear = c.get(Calendar.YEAR);
	     mMonth = c.get(Calendar.MONTH);
	     mDay = c.get(Calendar.DAY_OF_MONTH);
	     mHour = c.get(Calendar.HOUR_OF_DAY);
         mMinute = c.get(Calendar.MINUTE);
         
	     mDateDisplay = (TextView) findViewById(R.id.textData);
	     mTimeDisplay = (TextView) findViewById(R.id.horaComp);
		 
		Button data = (Button) findViewById(R.id.defData);
		data.setOnClickListener(new View.OnClickListener() {
				
			@Override
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		 });
		
		Button hora = (Button) findViewById(R.id.horarioComp);
		hora.setOnClickListener(new View.OnClickListener() {
				
			@Override
			public void onClick(View v) {
				showDialog(TIME_DIALOG_ID);
			}
		 });	
		
		Button back = (Button) findViewById(R.id.voltarPComp);
		back.setOnClickListener(new View.OnClickListener() {
				
			@Override
			public void onClick(View v) {
				Intent i = new Intent(NovoCompromissoActivity.this,GoogleCalendarActivity.class);
				i.putExtra("nelson", persistencia);
				i.putExtra("userName", userName);
				startActivity(i);
			}
		 });	
		
		Button salvar = (Button) findViewById(R.id.salvarCompr);
		salvar.setOnClickListener(new View.OnClickListener() {
				
			@Override
			public void onClick(View v) {
				
				EditText t = (EditText)findViewById(R.id.tituloComp);
				String titulo = t.getText().toString();
				
				String data = mDateDisplay.getText().toString();
				String hora = mTimeDisplay.getText().toString();
				
				EditText p = (EditText)findViewById(R.id.observacoesComp);
				String descricao = p.getText().toString();
				
				Calendario novoComp = new Calendario(titulo, descricao, data, hora);
				
				persistencia.inserirCalendario(novoComp);
				
				try {
					persistencia.guardaCalendar(persistencia.getListaCalendar(),userName);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
	            Intent i2 = new Intent(NovoCompromissoActivity.this,GoogleCalendarActivity.class); 
	            i2.putExtra("nelson", persistencia);
	            i2.putExtra("userName", userName);
				startActivity(i2);
			}
		 });	
	}
	 
    protected Dialog onCreateDialog(int id) {
     	    switch (id) {
       	    case DATE_DIALOG_ID:
       	        return new DatePickerDialog(this,
       	                    mDateSetListener,
       	                    mYear, mMonth, mDay);
       	     case TIME_DIALOG_ID:
       	    	 return new TimePickerDialog(this, 
       	    			 	mTimeSetListener, 
       	    			 	mHour, mMinute, false);
       	    }
     	    
       	    return null;
       	}
}
